<?php

namespace App\Http\Controllers;

use App\Library\EncryptDecrypt;
use App\Service\ChargeService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;

class ChargeController extends BaseController
{
    /**
     * 获取token
     * @param ChargeService $chargeService
     * @return \Illuminate\Http\JsonResponse
     */
    public function queryToken(ChargeService $chargeService)
    {
        $this->requestData['OperatorID'] = $this->OperatorID;
        $token = $chargeService->token($this->requestData, $this->getIvData($this->OperatorID));
        return $this->json($token);
    }

    /**
     * 充电认证
     * @param ChargeService $chargeService
     * @return \Illuminate\Http\JsonResponse
     */
    public function queryEquipAuth(ChargeService $chargeService)
    {
        $check = Validator::make($this->requestData, [
            'EquipAuthSeq' => 'required',
            'ConnectorID' => 'required',
        ], [
            'EquipAuthSeq.required' => '设备认证流水号必传',
            'OperatorID.required' => '充电设备接口编码必传',
        ]);
        if ($check->errors()->first()) {
            return $this->jsonData(4003, $check->errors()->first());
        }
        $this->requestData['OperatorID'] = $this->OperatorID;
        $equip = $chargeService->queryEquipAuth($this->requestData);
        return $this->json($equip);
    }

    /**
     * 开始充电
     * @param ChargeService $chargeService
     * @return \Illuminate\Http\JsonResponse
     */
    public function queryStartCharge(ChargeService $chargeService)
    {
        $info = Validator::make($this->requestData, [
            'StartChargeSeq' => 'required',
            'ConnectorID' => 'required',
        ], [
            'StartChargeSeq.required' => '充电订单号必传',
            'ConnectorID.required' => '充电设备接口编码必传'
        ]);
        if ($info->errors()->first()) {
            return $this->jsonData(4003, $info->errors()->first());
        }
        $chargeStart = $chargeService->queryStartCharge($this->requestData);
        $chargeStart['StartTime'] = date('Y-m-d H:i:s');
        return $this->json($chargeStart, 'notificationStartChargeResult');
    }

    /**
     * 结束充电
     * @param ChargeService $chargeService
     * @return \Illuminate\Http\JsonResponse
     */
    public function queryStopCharge(ChargeService $chargeService)
    {
        $info = Validator::make($this->requestData, [
            'StartChargeSeq' => 'required',
            'ConnectorID' => 'required',
        ], [
            'StartChargeSeq.required' => '充电订单号必传',
            'ConnectorID.required' => '充电设备接口编码必传'
        ]);
        if ($info->errors()->first()) {
            return $this->jsonData(4003, $info->errors()->first());
        }
        $authStop = $this->requestData;
        $authStop['OperatorID'] = $this->OperatorID;
        $stopCharge = $chargeService->queryStopCharge($authStop);
        $stopCharge['OperatorID'] = $this->OperatorID;
        $stopCharge['StartChargeSeqStat'] = 3;
        $stopCharge['ConnectorID'] = $authStop['ConnectorID'];
        return $this->json($stopCharge, 'notificationStopChargeResult');
    }

    /**
     * 查询充电状态
     * @param ChargeService $chargeService
     * @return \Illuminate\Http\JsonResponse|void
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    public function queryEquipChargeStatus(ChargeService $chargeService)
    {
        $info = Validator::make($this->requestData, [
            'StartChargeSeq' => 'required',
        ], [
            'OperatorID.required' => '充电订单号必传',
        ]);
        if ($info->errors()->first()) {
            return $this->jsonData(4003, $info->errors()->first());
        }
        $status = $chargeService->queryEquipChargeStatus($this->requestData);
        if (!empty($status)) {
            $getStatus = $this->getChargeStatusCache($status['ConnectorID']);
            $getStatus = json_decode($getStatus, true);
        }
        $status['ConnectorStatus'] = $getStatus['status'] ?? 1;
        $status['CurrentA'] = 0;
        $status['VoltageA'] = 0;
        return $this->json($status);
    }

}
